/* Created By TheBrodieMan */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using CommonBehaviors.Actions;
using System.Windows.Forms;
using System.Windows.Media;
using Styx;
using Styx.Common;
using Styx.CommonBot;
using Styx.CommonBot.Profiles;
using Styx.Pathing;
using Styx.TreeSharp;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;

using Honorbuddy.QuestBehaviorCore;

using Action = Styx.TreeSharp.Action;

namespace Honorbuddy.Quest_Behaviors.SpecificQuests.SaveTheChildren
{
	[CustomBehaviorFileName(@"25889-SaveTheChildren")]
	public class SaveTheChildren : CustomForcedBehavior
	{
		public SaveTheChildren(Dictionary<string, string> args)
			: base(args)
		{
		try
			{
				QuestId = 25889; //GetAttributeAsQuestId("QuestId", true, null) ?? 0;
			}
			catch
			{
				Logging.Write("Problem parsing a QuestId in behavior: Commandeer That Balloon");
			}
		}

		public int QuestId { get; set; }
		public int MobIdDruid = 41528;
		public int AuraIdScared = 49774;
		public int AuraIdRiding = 43671;
		public WoWPoint DruidPickUp = new WoWPoint(1608.036, 1589.798, 127.535);
		public WoWPoint DruidDropOff = new WoWPoint(2147.124, 1537.363, 343.1199);
		private bool _isBehaviorDone;
		public static LocalPlayer me = StyxWoW.Me;
		static public bool InVehicle { get { return Lua.GetReturnVal<int>("if IsPossessBarVisible() or UnitInVehicle('player') then return 1 else return 0 end", 0) == 1; } }
		
		public List<WoWUnit> ScaredDruid
		{
			get
			{
				return ObjectManager.GetObjectsOfType<WoWUnit>()
									.Where(u => (u.Entry == MobIdDruid && u.IsAlive && u.HasAura(AuraIdScared)))
									.OrderBy(u => u.Distance).ToList();
			}
		}
		
		public override bool IsDone
		{
			get
			{
				return _isBehaviorDone;
			}
		}
		
		private LocalPlayer Me
		{
			get { return (StyxWoW.Me); }
		}
		
		public bool IsQuestComplete()
		{
			var quest = StyxWoW.Me.QuestLog.GetQuestById((uint)QuestId);
			return quest == null || quest.IsCompleted;
		}
		
		public override void OnStart()
		{
			OnStart_HandleAttributeProblem();
			if (!IsDone)
			{
				if (TreeRoot.Current != null && TreeRoot.Current.Root != null && TreeRoot.Current.Root.LastStatus != RunStatus.Running)
				{
					var currentRoot = TreeRoot.Current.Root;
					if (currentRoot is GroupComposite)
					{
						var root = (GroupComposite)currentRoot;
						root.InsertChild(0, CreateBehavior());
					}
				}
				PlayerQuest Quest = Me.QuestLog.GetQuestById((uint)QuestId);
				TreeRoot.GoalText = ((Quest != null) ? ("\"" + Quest.Name + "\"") : "In Progress");
			}
		}
		
		public Composite DoneYet
		{
			get
			{
				return
					new Decorator(ret => IsQuestComplete(), new Action(delegate
					{
						TreeRoot.StatusText = "Finished!";
						_isBehaviorDone = true;
						return RunStatus.Success;
					}));
			}
		}
		
		public WoWUnit tar = null;
		
		public Composite DropOffPickUp
		{
			get
			{
				return new PrioritySelector(
					new Action(context =>
					{
						TreeRoot.StatusText = "Checking for passenger";
						var rider =
							ObjectManager.GetObjectsOfType<WoWUnit>().Where(r => 
								r.Entry == MobIdDruid && r.IsAlive && r.HasAura(AuraIdRiding)).OrderBy(r=>
								r.Distance).FirstOrDefault();
						if (rider != null)
						{
							tar = rider;
							return RunStatus.Failure;
						}
						else if (ScaredDruid != null)
						{
							tar = ScaredDruid.FirstOrDefault();
							return RunStatus.Failure;
						}
						else
						{
							Logging.Write("No rider with us, go get one.");
							return RunStatus.Failure;
						}
					}),
					new Decorator(context => tar == null,
						new Action(context =>
						{
							TreeRoot.StatusText = "Going to Grab a Druid";
							if (!Me.HasAura(54170))
								Lua.DoString("CastPetAction(3)");
							Flightor.MoveTo(DruidPickUp);
						})),
					new Decorator(context => tar.HasAura(AuraIdRiding),
						new Action(context =>
						{
							if (Me.Location.Distance(DruidDropOff) > 5)
							{
								TreeRoot.StatusText = "Flying a Druid Back Home";
								if (!Me.HasAura(54170))
									Lua.DoString("CastPetAction(3)");
								Flightor.MoveTo(DruidDropOff);
							}
							else
							{
								TreeRoot.StatusText = "Dropping a Druid off";
								Lua.DoString("CastPetAction(2)");
								Thread.Sleep(3000);
							}
						})),
					new Decorator(context => tar.HasAura(AuraIdScared),
						new Action(context =>
						{
							TreeRoot.StatusText = "Grabbing a Druid";
							Flightor.MoveTo(tar.Location);
							Lua.DoString("CastPetAction(1)");
							Thread.Sleep(2000);
						})));
			}
		}

		private Composite _root;
		protected override Composite CreateBehavior()
		{
			return _root ?? (_root = new Decorator(ret => !_isBehaviorDone,
				new PrioritySelector(
					new Decorator(context => !Me.HasAura(77630),
						new Action(context => { _isBehaviorDone = true; })),
					DoneYet, DropOffPickUp, new ActionAlwaysSucceed())));
		}
	}
}

